<?php

use Illuminate\Database\Seeder;
use App\Menu;

class MenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 清理菜单数据
        $connection = new \MongoDB\Client('mongodb://'.env('DB_HOST'));
        $collection = $connection->selectCollection(env('DB_DATABASE'), 'menus');
        $collection->drop();

        $file = database_path('seeds/menus.yml');
        $raw = yaml_parse_file($file);
        Menu::unguard();
        foreach($raw as $i => $item) {
            /**@var Menu $menu **/
            $menu = Menu::firstOrCreate([
               'key' => $item['key']
            ]);
            $menu->update($item);
        }
    }
}
